package com.bra.ui.activity;

import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bra.R;
import com.bra.bean.ReceiverMsg;
import com.bra.ui.adapter.MsgAdapter;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.Contacts;
import com.bra.utils.JsonUtils;
import com.bra.utils.SPHelper;
import com.bra.utils.ToastUtil;
import com.google.gson.Gson;

/**
 * 我的消息
 * @author 我的消息
 *
 */
public class MyMsgListActivity extends BaseActivity {
	private ImageView activity_back;
	private TextView activity_title;
	private ListView ll_native;
	private MsgAdapter adapter;	
	private String msg_list ;
	private List<ReceiverMsg> receive_list;
	private int noreads;
	private Gson gson;
	private HashMap<String,List<ReceiverMsg>> data;
	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_msglist, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("消息");
		gson = new Gson();
		data = new HashMap<String,List<ReceiverMsg>>();
		ll_native = (ListView) findViewById(R.id.ll_native);
		adapter = new MsgAdapter(MyMsgListActivity.this);
		ll_native.setAdapter(adapter);	
		getMsg();
	}
		/****获取消息列表****/
	private void getMsg() {
		noreads = SPHelper.readInt(MyMsgListActivity.this, Contacts.JPUSH_NOREAN, 0);
		msg_list = SPHelper.readString(MyMsgListActivity.this, Contacts.JPUSH_MSG);
		if (msg_list==null) {
			ToastUtil.showToast(MyMsgListActivity.this, "您暂时没有消息");
		}else{
			receive_list = JsonUtils.fromJson(msg_list, ReceiverMsg.class, "msg_list");
		}
		if (receive_list!=null&&receive_list.size()>0) {
			adapter.setData(receive_list);
		}
	}


	@Override
	protected void setListener() {
		activity_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		ll_native.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				for (int i = 0; i < receive_list.size(); i++) {
					ReceiverMsg read_thi = receive_list.get(i);
					read_thi.is_select=false;
				}
				ReceiverMsg read_this = receive_list.get(arg2);
				read_this.is_select =true;
				read_this.is_read="1";
				noreads--;
				adapter.notifyDataSetChanged();
			}
		});
	}
	@Override
	protected void onPause() {
		data.put("msg_list", receive_list);
		final String json = gson.toJson(data);
		//查看未读消息数
		new Thread(new Runnable() {
			@Override
			public void run() {
				SPHelper.saveInt(MyMsgListActivity.this, Contacts.JPUSH_NOREAN, noreads);
				SPHelper.saveString(MyMsgListActivity.this, Contacts.JPUSH_MSG, json);	
			}
		}).start();
		super.onPause();
	}
}
